<template>
  <div>
    <div>
      <el-input style="width: 200px" placeholder="查询权限类型" v-model="searchPermissionType"></el-input>
      <el-button type="primary" @click="loadPermissions(1)">查询</el-button>
      <el-button type="info" @click="resetSearch">重置</el-button>
    </div>
    <div style="margin: 10px 0">
      <el-button type="primary" plain @click="handleAdd">新增权限</el-button>
      <el-button type="danger" plain @click="delBatch">批量删除</el-button>
    </div>
    <el-table :data="permissionsData" stripe :header-cell-style="{ backgroundColor: 'aliceblue', color: '#666' }" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"></el-table-column>
      <el-table-column prop="permissionid" label="权限ID" width="70" align="center"></el-table-column>
      <el-table-column prop="userid" label="用户ID"></el-table-column>
      <el-table-column prop="classid" label="班级ID"></el-table-column>
      <el-table-column prop="permissiontype" label="权限类型"></el-table-column>
      <el-table-column label="操作" align="center" width="180">
        <template v-slot="scope">
          <el-button size="mini" type="primary" plain @click="handleEdit(scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" plain @click="del(scope.row.permissionid)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="margin: 10px 0">
      <el-pagination
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="pageSize"
          layout="total, prev, pager, next"
          :total="total">
      </el-pagination>
    </div>
    <el-dialog title="权限信息" :visible.sync="dialogVisible" width="30%">
      <el-form :model="form" label-width="80px" style="padding-right: 20px" :rules="rules" ref="formRef">
        <el-form-item label="用户ID" prop="userid">
          <el-input v-model="form.userid" placeholder="用户ID"></el-input>
        </el-form-item>
        <el-form-item label="班级ID" prop="classid">
          <el-input v-model="form.classid" placeholder="班级ID"></el-input>
        </el-form-item>
        <el-form-item label="权限类型" prop="permissiontype">
          <el-input v-model="form.permissiontype" placeholder="权限类型"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "Permissions",
  data() {
    return {
      permissionsData: [],  // 所有的权限数据
      pageNum: 1,           // 当前的页码
      pageSize: 10,         // 每页显示的个数
      searchPermissionType: '',
      total: 0,
      dialogVisible: false,
      form: {},
      selectedIds: [],
      rules: {
        permissiontype: [
          { required: true, message: '请输入权限类型', trigger: 'blur' },
          { min: 1, max: 50, message: '权限类型长度在 1 到 50 个字符', trigger: 'blur' }
        ],
        userid: [
          { required: true, message: '请输入用户ID', trigger: 'blur' }
        ],
        classid: [
          { required: true, message: '请输入班级ID', trigger: 'blur' }
        ]
      }
    };
  },
  created() {
    this.loadPermissions();
  },
  methods: {
    delBatch() {
      // ... 批量删除方法 ...
    },
    handleSelectionChange(rows) {
      this.selectedIds = rows.map(v => v.permissionid);
    },
    del(permissionid) {
      // ... 删除方法 ...
    },
    handleEdit(row) {
      this.form = JSON.parse(JSON.stringify(row)); // 深拷贝
      this.dialogVisible = true;
    },
    handleAdd() {
      this.form = {}; // 清空表单
      this.dialogVisible = true;
    },
    save() {
      // ... 保存方法 ...
    },
    resetSearch() {
      this.searchPermissionType = '';
      this.loadPermissions();
    },
    handleCurrentChange(pageNum) {
      this.loadPermissions(pageNum);
    },
    async loadPermissions(pageNum) {
      if (pageNum) this.pageNum = pageNum;
      try {
        const res = await this.$request.get('/permissions/selectByPage', {
          params: {
            pageNum: this.pageNum,
            pageSize: this.pageSize,
            permissiontype: this.searchPermissionType
          }
        });
        this.permissionsData = res.data.records || [];
        this.total = res.data.total || 0;
      } catch (error) {
        console.error('请求权限数据失败', error);
        this.permissionsData = [];
        this.total = 0;
        this.$message.error('加载权限数据失败');
      }
    },
  },
};
</script>

<style scoped>
/* Add your CSS styles here */
</style>